﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MT.Enterprise.Utils.Extensions;
using NP.BPMReportPlatform.BPMPlatform;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Engine;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.TalentProfile;
using NP.BPMReportPlatform.TalentProfile.Dto;
using NP.BPMReportPlatform.Utils;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SqlSugar;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.TalentProfile
{
    /// <summary>
    /// 试用期服务
    /// </summary>
    public class TalentProfileService : BPMReportPlatformAppService, ITalentProfileService
    {
        #region Variables

        private readonly SqlSugarClient _db;
        private readonly SqlSugarClient _dbPlatform;
        private readonly IBPMUtilsAppService _bPMUtilsApp;
        private readonly IBPMPlatformAppService _iBPMPlatformApp;
        private readonly SqlSugarClient _dbEngine;

        #endregion

        #region Constructor

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sqlSugarAppService"></param>
        public TalentProfileService(ISqlSugarAppService sqlSugarAppService, IBPMUtilsAppService bPMUtilsApp
            , IBPMPlatformAppService iBPMPlatformApp)
        {
            _db = sqlSugarAppService.GetBusinessInstance();
            _dbPlatform = sqlSugarAppService.GetPlatformInstance();
            _dbEngine = sqlSugarAppService.GetEngineInstance();
            _bPMUtilsApp = bPMUtilsApp;
            _iBPMPlatformApp = iBPMPlatformApp;
        }

        #endregion


        /// <summary>
        /// 试用期员结果汇总查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<GetTalentProfileResultOutput>> GetTalentProfileResult(GetTalentProfileResultInput input)
        {
            RefAsync<int> totalCount = 0;

            var listQueryable = _db.Queryable<TalentProfileEntity>()
                .Where(t => t.Status == 4)
                .WhereIF(!input.InstanceNumber.IsNullOrEmpty(), t => t.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(!input.OldInstanceNumber.IsNullOrEmpty(), t => t.OldInstanceNumber.Contains(input.OldInstanceNumber))
                .WhereIF(!input.JobName.IsNullOrEmpty(), t => t.JobName.Contains(input.JobName))
                .WhereIF(!input.JobCategroy.IsNullOrEmpty(), t => t.JobCategroy.Contains(input.JobCategroy))
                .WhereIF(!input.DepartmentName.IsNullOrEmpty(), t => t.DepartmentName.Contains(input.DepartmentName))
                .OrderBy(t => t.InstanceNumber, OrderByType.Desc)
                .Select(t =>
                    new GetTalentProfileResultOutput()
                    {
                        InstanceNumber = t.InstanceNumber,
                        OldInstanceNumber = t.OldInstanceNumber,
                        ExternalCode = t.ExternalCode,
                        JobName = t.JobName,
                        JobCategroy = t.JobCategroy,
                        DepartmentName = t.DepartmentName,
                        CreateWorkNumber = t.CreateWorkNumber,
                        CreateUserName = t.CreateUserName,
                        CreateTime = t.CreateTime,
                        UpdateWorkNumber = t.UpdateWorkNumber,
                        UpdateUserName = t.UpdateUserName,
                        UpdateTime = t.UpdateTime,
                        JobType = t.JobType,
                        CustJobcodeGroupName = t.CustJobcodeGroupName,

                    });
            var listViewDto = await listQueryable.ToPageListAsync(input.PageIndex, input.PageSize, totalCount);

            //返回结果
            return new PagedResultDto<GetTalentProfileResultOutput>()
            {
                TotalCount = totalCount,
                Items = listViewDto
            };
        }


    }
}